/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AcessDB;

/**
 *
 * @author bj
 */
public class Acess {
    
    private java.sql.Connection con = null;
    private final String url = "jdbc:sqlserver://";
    private final String serverName = "localhost";
    private final String portNumber = "1433";
    private final String databaseName = "TracNghiem";
    private final String userName = "sa";
    private final String password = "123456";
    // Informs the driver to use server a side-cursor,
    // which permits more than one active statement
    // on a connection.
    private final String selectMethod = "cursor";

    // Constructor
    public Acess() {
    }

    private String getConnectionUrl() {
            return url + serverName + ":" + portNumber + ";databaseName="
                            + databaseName + ";selectMethod=" + selectMethod + ";";
    }

    public java.sql.Connection getConnection() {
            try {
                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName, password);
                    //con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName, password);
                    if (con != null)
                            System.out.println("Connection Successful!");
            } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("Error Trace in getConnection() : "
                                    + e.getMessage());
            }
            return con;
    }
    public void closeConnection() {
        try 
        {
                if (con != null)
                        con.close();
                con = null;
        } 
        catch (Exception e) 
        {
                e.printStackTrace();
        }
    }
}
